package com.yunwaikeji.gy_tool_library.utils.picture.download;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;


import java.io.File;

/**
 * 图片缩放工具
 */
public class GYPictureZoomUtils {
    public static GYPictureZoomUtils newBuilder(Bitmap bitmap) {
        return new GYPictureZoomUtils(bitmap);
    }

    public static GYPictureZoomUtils newBuilder(Drawable drawable) {
        return newBuilder(GYDisplayUtils.toBitmap(drawable));
    }

    public static GYPictureZoomUtils newBuilder(Context context, int resId) {
        return newBuilder(GYDisplayUtils.getBitmap(context, resId));
    }

    public static GYPictureZoomUtils newBuilder(String filePath) {
        return newBuilder(GYDisplayUtils.getBitmap(filePath));
    }

    public static GYPictureZoomUtils newBuilder(File filePath) {
        return newBuilder(BitmapFactory.decodeFile(filePath.getPath()));
    }

    private final Bitmap bitmap;
    private final int bWidth;
    private final int bHeight;
    private int rWidth;
    private int rHeight;

    public GYPictureZoomUtils(Bitmap bitmap) {
        this.bitmap = bitmap;
        bWidth = bitmap.getWidth();
        bHeight = bitmap.getHeight();
        rWidth = bWidth;
        rHeight = bHeight;
    }

    public GYPictureZoomUtils setSize(int width, int height) {
        rWidth = width;
        rHeight = height;
        return this;
    }

    public GYPictureZoomUtils setWidth(int width) {
        return setSize(width, bHeight);
    }

    public GYPictureZoomUtils setHeight(int height) {
        return setSize(bWidth, height);
    }

    public GYPictureZoomUtils setMaxLength(long maxLength) {
        long now = GYDisplayUtils.getBitmapLength(bitmap);
        if (now > maxLength) {
            return setLength(maxLength);
        }
        return this;
    }

    public GYPictureZoomUtils setLength(long length) {
        long now = GYDisplayUtils.getBitmapLength(bitmap);
        long nowArea = bWidth * bHeight;
        float proportion = (float) length / now;
        long area = (long) (nowArea * proportion);


        rWidth = (int) (bWidth * proportion);
        rHeight = (int) (bHeight * proportion);
        return this;
    }

    public GYPictureZoomUtils setInside(int width, int height) {
        if (width < bWidth || height < bHeight) {
            if (bWidth - width > bHeight - height) {
                rWidth = width;
                float proportion = (float) width / bWidth;
                rHeight = (int) (bHeight * proportion);
            } else {
                rHeight = height;
                float proportion = (float) height / bHeight;
                rWidth = (int) (bWidth * proportion);
            }
        }
        return this;
    }

    public GYPictureZoomUtils setOutside(int width, int height) {
        if (bWidth > width && bHeight > height) {
            if (bWidth - width > bHeight - height) {
                rHeight = height;
                float proportion = (float) rHeight / bHeight;
                rWidth = (int) (bWidth * proportion);
            } else {
                rWidth = width;
                float proportion = (float) rWidth / bHeight;
                rHeight = (int) (bHeight * proportion);
            }
        }
        return this;
    }

    public Bitmap build() {
        if (rWidth == bWidth && rHeight == bHeight) {
            return bitmap;
        }
        return GYDisplayUtils.toBitmap(GYDisplayUtils.newBuilder()
                .setSizePx(rWidth, rHeight)
                .setNormal(new BitmapDrawable(bitmap))
                .buildDrawable());
    }
}
